
//
//  UITabBarController+BMExtension.m
//  BMBlueMoonAngel
//
//  Created by Daredos on 16/2/17.
//  Copyright © 2016年 elvin. All rights reserved.
//

#import "UITabBarController+BMExtension.h"
#import "BMNavigationController.h"
@implementation UITabBarController (BMExtension)

- (UIViewController *)addViewControllerWithClass:(Class)clas
                                           title:(NSString *)title
                           selectBackgroundImage:(NSString *)selectImage
                           normalBackgroundImage:(NSString *)normalImage {
    
    
    //1.给传进来的包装一个导行栏
    UIViewController *vc = [[[clas class] alloc] init];
    vc.title = title;
    BMNavigationController *nc = [[BMNavigationController alloc] initWithRootViewController:vc];

    
    //2.创建dismiss按钮，返回到主菜单
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 12, 20);
    [backButton setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
    backButton.transform = CGAffineTransformMakeScale(1.2, 1.2);
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    vc.navigationItem.leftBarButtonItem = backItem;
    [backButton addTarget:self action:@selector(dismissClick) forControlEvents:UIControlEventTouchUpInside];

    
    
    //3.添加tabbar的子控制器
    nc.title = title;
    nc.tabBarItem.image = [UIImage imageNamed:normalImage];
    nc.tabBarItem.selectedImage  = [[UIImage imageNamed:selectImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    NSMutableArray *muarr = [NSMutableArray arrayWithArray:self.viewControllers];
    [muarr addObject:nc];
    self.viewControllers = muarr;
    return vc;
}

- (void)dismissClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
